package service

import (
	"go-web-scaffold/dao"
	"go-web-scaffold/dto"
)

type UserService struct {
	dao *dao.UserDao
}

func NewUserService(dao *dao.UserDao) *UserService {
	return &UserService{
		dao,
	}
}
func (_this *UserService) RegisterUser(userName string, password string) *dto.RegisterResult {
	user, error := _this.dao.GetUser(userName)
	if error != nil {
		return &dto.RegisterResult{
			Status: "error",
			Msg:    "用户注册发生错误",
		}
	}
	if user.Id != 0 {
		return &dto.RegisterResult{
			Status: "error",
			Msg:    "用户" + userName + "已经存在",
		}
	}
	u, e := _this.dao.AddUser(userName, password)
	if e != nil {
		return &dto.RegisterResult{
			Status: "error",
			Msg:    "用户注册发生错误",
		}
	}
	if u != nil && u.Id == 0 {
		return &dto.RegisterResult{
			Status: "success",
			Msg:    "恭喜您，用户" + userName + "注册成功",
		}
	}
	return &dto.RegisterResult{
		Status: "error",
		Msg:    "用户注册发生错误",
	}
}
